<?php
/**
 * Created by PhpStorm.
 * User: apple
 * Date: 2018/7/24
 * Time: 下午10:13
 */
namespace PddWechat\Models\Pos\Service;

use Dai\Framework\Base\BaseException;
use Dai\Framework\Base\BaseService;
use Dai\Framework\Base\BaseSessionInfo;
use PddWechat\Models\Pos\Data\PosActivateData;
use PddWechat\Models\Pos\Data\PostInviteData;
use PddWechat\Models\Pos\DataObject\PosActivateDo;
use PddWechat\Models\Pos\Param\PosActivateParam;

class PosActivate extends BaseService
{
    public function __construct()
    {
        $this->posActivateData = new PosActivateData();
        $this->postInvateData = new PostInviteData();
    }

    public function execute($basePageInfo)
    {
        /** @var BaseSessionInfo $sessionData */
        $sessionData = $basePageInfo->sessionInfo;
        $openid = $sessionData->openId;
        /** @var PosActivateParam $params */
        $params = $basePageInfo->params;
        //判断订单是否存在
        $postInvateInfo = $this->postInvateData->getApplyInfoByOrderNum($params->orderNum);
        if($postInvateInfo == null) {
            throw new BaseException(BaseException::POS_ORDER_NOT_EXIST);
        }
        //判断是否重复提交
        $posActivateInfo = $this->posActivateData->select($params->orderNum);
        if($posActivateInfo != null && ($posActivateInfo->status == 0 || $posActivateInfo->status == 1)) {
            throw new BaseException(BaseException::POS_ORDER_ALREADY_APPLY);
        }
        $posActiveDo = new PosActivateDo();
        $posActiveDo->openId = $openid;
        $posActiveDo->phone = $params->phone;
        $posActiveDo->machineNum = $params->machineNum;
        $posActiveDo->status = 0;
        $posActiveDo->orderNum = $params->orderNum;
        return $this->posActivateData->insert($posActiveDo);
    }
}